const App = getApp();
const navigationBarHeight = (getApp().diygwstatusBarHeight + 44) + 'px'
Page({
    data: {
        tabs1: ['商品简介', '商品评价'],
        activeIndex1: 0,
        scrollLeft: 0,
        num: 0,
        msgs1: [],
        url: App.Config.basePath,

    },

    //弹出提示信息
    showModal(message) {
        App.WxService.showModal({
            title: '友情提示',
            content: message,
            showCancel: !1,
        });
    },
    onShareAppMessage: function () {
    },

    //页面加载
    onLoad(option) {
        if (option) {
            this.setData({
                globalOption: option
            })
        }
    },

    //页面显示
    onShow() {
        this.getMsgs1();
        this.setData({
            num: wx.getStorageSync("num") || 0
        })
    },

    //页面跳转
    navigateTo(e) {
        App.navigateTo(e.currentTarget.dataset.url, e.currentTarget.dataset);
    },
    //tab切换事件
    tabClick1: function (e) {
        const dataset = e.currentTarget.dataset;
        this.setData({
            activeIndex1: dataset.id,
            scrollLeft: (e.currentTarget.dataset.id - 1) * 60
        });
    },

    getMsgs1() {
        //设置要传递的参数
        let param = {
            f: 3,
            id: this.data.globalOption.id,
            loadmsg: `正在加载中`
        };

        App.HttpService.getData(param, "/products_List").then(data => { //执行服务器Servlet
            this.setData({
                msgs1: data //把从服务器端得到的值赋值给数组
            });
        });
    },

    //增加图标中的数量
    addcart: function (e) {
        let num = this.data.num;
        num++;
        this.setData({
            num: num,
            hasCarts: true
        })
        //将商品信息放入缓存中
        var cartItems = wx.getStorageSync("cartItems") || []
        var exist = cartItems.find(function (el) {
            return el.id == e.target.dataset.id
        })
        //当购物车里已经存在该商品数量时加1
        if (exist) {
            exist.value = parseInt(exist.value) + 1
        } else {
            cartItems.push({
                id: e.target.dataset.id,
                title: e.target.dataset.title,
                image: e.target.dataset.image,
                price: e.target.dataset.price,
                value: 1,
                selected: true
            })
        }
        //弹窗显示
        wx.showToast({
            title: "加入购物车",
            duration: 1000
        })
        //更新缓存数据
        wx.setStorageSync("cartItems", cartItems);
        wx.setStorageSync("num", this.data.num);
    },
    oncart() {
        wx.switchTab({
            url: "/pages/carts/index"
        })
    },
    onpay() {
        if (this.data.num == 0) {
            this.showModal("请先购买商品")
        } else {
            var cost = 0;
            var cartItems = wx.getStorageSync("cartItems") || [];
            for (var i = 0; i < cartItems.length; i++) {
                cost += cartItems[i].value * cartItems[i].price
            }

            wx.setStorageSync("total", cost.toFixed(2))
            wx.navigateTo({
                url: "/pages/pay/index"
            })
        }
    },



})